package com.agree.util.excel.handlers.db;

import com.agree.util.excel.config.Configuration;
import com.agree.util.excel.entity.TableField;
import com.agree.util.excel.entity.TableInfo;
import com.agree.util.excel.entity.TableKey;
import com.agree.util.excel.enums.QuerySQL;
import com.agree.util.excel.enums.TableKeyType;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by wangmu on 2017/12/1.
 */
public class OracleDBHandler extends DBHandler {

    public OracleDBHandler(Configuration configuration) {
        super(configuration);
    }

    public OracleDBHandler(String driveClassName, String url, String username, String password) {
        super(driveClassName, url, password, username);
    }

    @Override
    protected ResultSet getTableInfoResult(String tableName) throws SQLException {
        return this.commonQuery(QuerySQL.ORACLE.getTablesSql(),tableName);
    }

    @Override
    protected ResultSet getTableFieldsResult(String tableName) throws SQLException {
        return this.commonQuery(QuerySQL.ORACLE.getTableFieldsSql(),tableName);
    }

    @Override
    protected synchronized TableInfo getTableInfo(String tableName) {
        TableInfo tableInfo = new TableInfo();
        ResultSet tableInfoResult = null;
        ResultSet tableFieldsResult = null;
        //获取表信息
        try {
            tableInfoResult = this.getTableInfoResult(tableName);
            while(tableInfoResult.next()) {
                String name = tableInfoResult.getString(QuerySQL.ORACLE.getTableName());
                String tableComment = tableInfoResult.getString(QuerySQL.ORACLE.getTableComment());
                tableInfo.setName(name);
                tableInfo.setComment(tableComment);
                //判断表前缀
                if(configuration != null) {
                    String prefix = Arrays.stream(configuration.dbConfig().commonPrefix())
                            .filter(p -> name.toUpperCase().startsWith(p.toUpperCase()))
                            .findFirst()
                            .orElseGet(() -> name);
                    tableInfo.setPrex(prefix);
                }
            }

            //获取字段信息
            tableFieldsResult = this.getTableFieldsResult(tableName);
            //字段集合
            List<TableField> fields = new ArrayList<>();
            List<TableField> keys = new ArrayList<>();
            //主键集合
            while(tableFieldsResult.next()) {
                TableField tableField = new TableField();
                String name = tableFieldsResult.getString(QuerySQL.ORACLE.getFieldName());
                String comment = tableFieldsResult.getString(QuerySQL.ORACLE.getFieldComment());
                String type = tableFieldsResult.getString(QuerySQL.ORACLE.getFieldType());
                String colMust = tableFieldsResult.getString(QuerySQL.ORACLE.getFieldIsNullable());
                tableField.setName(name);
                tableField.setComment(comment);
                tableField.setType(type);
                tableField.setMust(colMust);
                //判断是否是主键
                String key = tableFieldsResult.getString(QuerySQL.ORACLE.getFieldKey());
                if(key != null && key.toUpperCase().equals("PRI")) {
                    keys.add(tableField);
                }
                fields.add(tableField);
            }
            //设置主键
            TableKey tableKey = new TableKey();
            if(keys.size()>1) {
                tableKey.setTableKeyType(TableKeyType.COMPOSITE);
            }
            tableKey.setKeysField(keys);
            tableInfo.setFields(fields);
            tableInfo.setTableKey(tableKey);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭连接
                this.closeConnection();
                tableInfoResult.close();
                tableFieldsResult.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return tableInfo;
    }

    @Override
    protected List<TableInfo> getAllTableInfo(String[] tableNames) {
        //获取所有表名
        return Arrays.asList(tableNames)
                .parallelStream()
                .map(t -> this.getTableInfo(t))
                .collect(Collectors.toList());
    }

    @Override
    public List<TableInfo> getAllTableInfo() {
        String[] includTables = super.configuration.dbConfig().includTables();
        return getAllTableInfo(includTables);
    }
}
